﻿#include "GameWorld.h"
#include "3dMath/ss3dMathFunction.h"
#include "tools/ssProcessInfo.h"
#include "element/camera/ssThirdPersonCamera.h"
#include "element/render/ssMeshRender.h"
#include "element/material/ssMaterial.h"
#include "element/material/ssTextureMaterial.h"
#include "element/render/ssAxesRender.h"
//#include "element/render/ssColorRender.h"
#include "element/render/ssBoardRender.h"
#include "shader/ssShaderManager.h"
#include "console/ssConsole.h"
#include "node/ssNodeFactory.h"
#include "resmgr/ssResManager.h"

GameWorld::GameWorld()
{
	m_tpCamera = 0;
	ssProcess::instance()->m_resPath = "../../res/";
}

GameWorld::~GameWorld(void)
{
}

void GameWorld::Init()
{
	SS_CONSOLE->Print("init game world...\n");

	SS_OPENGL->SetCullMode(SS_CULL_NULL);

	StarSeeker::ssShaderManager::instance()->CreateDefaultShader();
	SS_RES_MANAGR->CreateDefaultMesh();

	ssFLOAT4x4X proj = StarSeeker::Math3D::PerspectiveFovRH(45.0f, (ssfloat)SS_OPENGL->m_nWidth / (ssfloat)SS_OPENGL->m_nHeight, 1.0f, 100000.0f);
	ssNode* cameraNode1 = SS_NODE_FACTORY->CreateThirdPersonCamera();
	m_tpCamera = SS_DYNAMIC_CAST(ssThirdPersonCamera*, cameraNode1->GetComponent(ssComponent::SS_COMPONENT_CAMERA));
	m_tpCamera->SetProjMatrix(proj);
	m_tpCamera->SetCoord(0.0f, 0.0f, 0.0f);
	m_scene.RootNode().AddChild(cameraNode1);
	m_tpCamera->SetMainCamera();

	//f32 v[3 * 4] = { -10.0, 10.0, 0.0, -10.0, -10.0, 0.0, 10.0, -10.0, 0.0, 10.0, 10.0, 0.0 };
	//u16 f[3 * 2] = { 0, 1, 2, 0, 2, 3 };
	//StarSeeker::ssNode* pBallEn = new StarSeeker::ssNode("矩形");
	//StarSeeker::ssMeshRender* pRender = new StarSeeker::ssMeshRender();
	//StarSeeker::ssMaterial* pMaterial = new StarSeeker::ssMaterial();
	//pRender->SetVertex(3 * 4, v);
	//pRender->SetIndex(3 * 2, f);
	//StarSeeker::ssShader* ptestShader = StarSeeker::ssShaderManager::instance()->Find("base");
	//pMaterial->SetShader(ptestShader);
	//pBallEn->AddElement(pRender);
	//pBallEn->AddElement(pMaterial);
	//m_scene.RootNode().AddChild(pBallEn);

	//f32 v[6*3]= {0.000000,0.000000,50.000000, -0.000002,50.000000,-0.000002, -50.000000,-0.000004,-0.000002, 0.000001,-50.000000,-0.000002, 50.000000,0.000009,-0.000002, 0.000000,0.000000,-50.000000};
	//u16 f[8*3] = {0,1,2,0,2,3,0,3,4,0,4,1,5,2,1,5,3,2,5,4,3,5,1,4};
	//f32 c[6*4] = {0.0,0.0,1.0,1.0, 0.0,1.0,0.0,1.0, 1.0,1.0,1.0,1.0, 1.0,1.0,1.0,1.0, 1.0,0.0,0.0,1.0, 1.0,1.0,1.0,1.0};
	//StarSeeker::ssNode* pBallEn = new StarSeeker::ssNode("球体");
	//StarSeeker::ssColorRender* pElement = new StarSeeker::ssColorRender();
	//StarSeeker::ssMaterial* pMaterial = new StarSeeker::ssMaterial();
	//pElement->SetVertex(6 * 3, v);
	//pElement->SetIndex(8 * 3, f);
	//pElement->SetColor(6 * 4, c);
	//StarSeeker::ssShader* ptestShader = StarSeeker::ssShaderManager::instance()->Find("color");
	//pMaterial->SetShader(ptestShader);
	//pBallEn->AddElement(pElement);
	//pBallEn->AddElement(pMaterial);
	//m_scene.RootNode().AddChild(pBallEn);

	StarSeeker::ssNode* pAxesRE = SS_NODE_FACTORY->CreateAxesNode();
	m_scene.RootNode().AddChild(pAxesRE);

	StarSeeker::ssNode* pTextureBdNode = SS_NODE_FACTORY->CreateBoardNode("WM.tga");
	m_scene.RootNode().AddChild(pTextureBdNode);

	StarSeeker::ssNode* pBillboardNode = SS_NODE_FACTORY->CreateBillboardNode("UINV.tga");
	m_scene.RootNode().AddChild(pBillboardNode);

	StarSeeker::ssNode* pTextureMdNode = SS_NODE_FACTORY->CreateTextureModelNode("yunshi_01 DM.tga", "yunshi001.OBJEX");
	m_scene.RootNode().AddChild(pTextureMdNode);

	StarSeeker::ssNode* pTeapotNode = SS_NODE_FACTORY->CreateModelNode("teapot.obj");
	pTextureMdNode->AddChild(pTeapotNode);

	ssQuaternion qt;

	pTextureBdNode->Transform().SetPosition(100.0f, 0.0f, 0.0f);
	qt.SetAxisAngle(ssVector3(0, 0, 1), Math3D::gc_HALF_PI);
	pTextureBdNode->Transform().SetRotation(qt);

	//qt.SetEuler(0, 0, Math3D::gc_QUARTER_PI);
	//pAxesRE->Transform().SetRotation(qt);
	//pAxesRE->Transform().SetScaling(0.5f, 0.5f, 0.5f);

	pBillboardNode->Transform().SetPosition(-120.0f, 0.0f, 0.0f);

	pTextureMdNode->Transform().SetPosition(0.0f, 100.0f, 0.0f);
	pTeapotNode->Transform().SetPosition(0.0f, 0.0f, -100.0f);
}

void GameWorld::OnMouseMove(i32 x, i32 y)
{
	m_tpCamera->RotatingEye(x, y);
}

void GameWorld::OnMouseWheel(i32 w)
{
	m_tpCamera->ChangeFocus((ssfloat)w);
}

void GameWorld::OnKeyDown(i32 k)
{
	switch(k)
	{
	//case 65://a
	//	//m_pPlayerModel->Move(-40.f,0.f,0.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,0);
	//	break;
	//case 68://d
	//	//m_pPlayerModel->Move(40.f,0.f,0.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,1);
	//	break;
	//case 87://w
	//	//m_pPlayerModel->Move(0.f,40.f,0.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,2);
	//	break;
	//case 83://s
	//	//m_pPlayerModel->Move(0.f,-40.f,0.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,3);
	//	break;
	//case 81://q
	//	//m_pPlayerModel->Move(0.f,0.f,40.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,4);
	//	break;
	//case 69://e
	//	//m_pPlayerModel->Move(0.f,0.f,-40.f);
	//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
	//	//CollisionTest();
	//	m_pPlayerModel->SetKeyState(1,5);
	//	break;
	//case 90://z
	//	m_pPlayerModel->SetKeyState(1,6);
	//	break;
	//case 88://x
	//	m_pPlayerModel->SetKeyState(1,7);
	//	break;
    case 82://r
		m_tpCamera->ChangeFocus(40.f);
		break;
	case 70://f
		m_tpCamera->ChangeFocus(-40.f);
		break;
	}
}

void GameWorld::OnKeyUp(i32 k)
{
}

void GameWorld::Render()
{
	m_scene.RenderScene();

	/*static f32 v[3 * 3] = { 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, };
	static u32 m_handle = 0;
	static StarSeeker::ssShader* ptestShader = 0;
	static int m_glslID = -1;
	if (m_handle==0)
	{
	ptestShader = StarSeeker::ssShaderManager::instance()->Find("test");
	glGenBuffers(1, &m_handle);
	glBindBuffer(GL_ARRAY_BUFFER, m_handle);
	glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, SS_VBO_STATIC);
	ptestShader->Use();
	m_glslID = ptestShader->GetAttribLocation("inPosition");
	if (m_glslID >= 0) //传递数据到显存
	{
	glEnableVertexAttribArray(m_glslID);
	glVertexAttribPointer(m_glslID, 3, GL_FLOAT, GL_FALSE, 0, 0);
	}
	}
	//glVertexPointer(3, GL_FLOAT, 0, 0);
	glDrawArrays(SS_PRIMITIVE_TRIANGLE, 0, 3);*/
}
